#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstring>
using namespace std;

int main()
{
    const char *filename = "./connFifo";
    /* 判断管道文件是否存在 */
    int ret = access(filename, F_OK);
    if (ret != 0)
    {
        /* 创建文件 */
        ret = mkfifo(filename, 0644);
        if (ret == -1)
        {
            perror("mkfifo error:");
            exit(-1);
        }
    }

    /*1.打开文件*/
    int fd = open(filename, O_RDWR);
    if (fd == -1)
    {
        perror("open error:");
        _exit(-1);
    }
    /*2.读取数据*/
    int readBytes = 0;
    char buffer[11] = {0};
    while (1)
    {
        readBytes = read(fd, buffer, sizeof(buffer));
        if (readBytes < 0)
        {
            perror("read perror:");
            _exit(-1);
        }
        cout << "readBytes:" << readBytes << ", buffer" << buffer << endl;
    }

    /*3.关闭文件*/
    close(fd);
    return 0;
}